#include <iostream>
#include "include/high_precision_timer.hpp"
#include "include/timer.hpp"
#include "include/task.hpp"

using namespace qOS;

// Simple task function
void myTask(void* p) {
    static HighPrecisionTimer timer;
    
    if (timer.reloadIfExpired(500_ms)) {
        std::cout << "Task executed every 500ms" << std::endl;
    }
}

int main() {
    std::cout << "QuarkTS Static Library Usage Example" << std::endl;
    std::cout << "====================================" << std::endl;
    
    // Test HighPrecisionTimer functionality
    HighPrecisionTimer hptimer;
    
    // Set a timer for 1 second
    hptimer.set(1_sec);
    std::cout << "HighPrecisionTimer set for 1 second" << std::endl;
    
    // Test basic timer functionality
    timer basicTimer(200_ms);
    std::cout << "Basic timer set for 200ms" << std::endl;
    
    // Test microsecond precision delay
    std::cout << "Testing microsecond precision delay..." << std::endl;
    uint64_t start = HighPrecisionTimer::getMicroseconds();
    HighPrecisionTimer::delayMicroseconds(1000); // 1ms delay
    uint64_t end = HighPrecisionTimer::getMicroseconds();
    uint64_t actualDelay = end - start;
    
    std::cout << "Requested delay: 1000 microseconds" << std::endl;
    std::cout << "Actual delay: " << actualDelay << " microseconds" << std::endl;
    std::cout << "Precision error: " << (actualDelay - 1000) << " microseconds" << std::endl;
    
    // Test functional safety features
    std::cout << "\nTesting functional safety features..." << std::endl;
    
    // Test error state handling
    HighPrecisionTimer safetyTimer;
    bool result = safetyTimer.set(0); // Invalid time
    std::cout << "Setting timer with 0 time: " << (result ? "SUCCESS" : "FAILED (expected)") << std::endl;
    std::cout << "Error state: " << (safetyTimer.hasError() ? "ERROR" : "OK") << std::endl;
    
    // Clear error and set valid time
    safetyTimer.clearError();
    result = safetyTimer.set(100_ms);
    std::cout << "Setting timer with 100ms: " << (result ? "SUCCESS" : "FAILED") << std::endl;
    std::cout << "Error state: " << (safetyTimer.hasError() ? "ERROR" : "OK") << std::endl;
    
    // Test boundary conditions
    std::cout << "\nTesting boundary conditions..." << std::endl;
    
    // Test maximum timer interval
    result = safetyTimer.set(HighPrecisionTimer::MAX_TIMER_INTERVAL + 1);
    std::cout << "Setting timer beyond MAX_TIMER_INTERVAL: " 
              << (result ? "SUCCESS" : "FAILED (expected)") << std::endl;
    
    // Test compatibility with existing timer interface
    std::cout << "\nTesting compatibility with existing timer interface..." << std::endl;
    
    HighPrecisionTimer compatTimer(300_ms);
    
    // Test all operator overloads
    compatTimer = 400_ms;  // Assignment operator
    bool expired = compatTimer();  // Function call operator
    compatTimer = true;     // Boolean assignment (reload)
    
    std::cout << "Timer interval: " << compatTimer.getInterval() << " microseconds" << std::endl;
    std::cout << "Timer status: " << (compatTimer.status() ? "ARMED" : "DISARMED") << std::endl;
    
    std::cout << "\nExample completed successfully!" << std::endl;
    std::cout << "The QuarkTS static library is working correctly." << std::endl;
    
    return 0;
}